﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIShell.OSGi;
using Pasys.PresentationCore;
using GeneratorLib.DB;
using GeneratorLib.DB.Schemas;
using Pasys.PresentationCore.WinForms;
using Pasys.PresentationCore.DockShell;

namespace DBExplorer
{
    public partial class CopyMenu : Form, ILayoutViewProxyContainer// GeneratorLib.IGenerator
    {

        private ShellPanelLayoutView layoutView;
        public IShellLayoutViewProxy LayoutViewProxy
        {
            get
            {
                return layoutView.LayoutViewProxy;
            }
        }
        public string SessionKey
        {
            get
            {
                if (layoutView.LayoutViewProxy != null)
                {
                    return LayoutViewProxy.SessionKey;
                }
                else
                {
                    return string.Empty;
                }
            }
        }
        public IWorkspace Workspace
        {
            get
            {
                return null;
            }
        }

        public CopyMenu()
        {
            InitializeComponent();
        }

        public void ShowLayoutIvew(ShellPanelLayoutView view)
        {
            layoutView = view;
            Controls.Clear();
            Controls.Add(view);
            view.Dock = DockStyle.Fill;
        }

        private void RootForm_Activated(object sender, EventArgs e)
        {
            RegistProxyContainer();
        }

        private void RegistProxyContainer()
        {
            var List = BundleRuntime.Instance.GetService<ILayoutViewProxyContainer>();
            for (int i = List.Count - 1; i >= 0; i--)
            {
                BundleRuntime.Instance.RemoveService<ILayoutViewProxyContainer>(List[i]);
            }
            var con = BundleRuntime.Instance.GetFirstOrDefaultService<ILayoutViewProxyContainer>();
            if (con != null)
            {
                BundleRuntime.Instance.RemoveService<ILayoutViewProxyContainer>(con);
            }
            BundleRuntime.Instance.AddService<ILayoutViewProxyContainer>(this);

            List = BundleRuntime.Instance.GetService<ILayoutViewProxyContainer>();
            if (List.Count != 1)
            {
                throw new Exception();
            }
        }

        private void CopyMenu_Load(object sender, EventArgs e)
        {
            RegistProxyContainer();
        }



    }


    public class CopyMenuProvider : AbstractViewProvider
    {
        protected override Control CreateControl()
        {
            CopyMenu f = new CopyMenu();
            ShellPanelLayoutView layoutView = new ShellPanelLayoutView();
            f.ShowLayoutIvew(layoutView);
            if (this.Onwer != null)
            {
                f.Show();
            }
            else
            {
                f.ShowDialog();
            }
            return null;
        }
    }


}
